package com.example.distillapplication.ui.fragment.set;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.distillapplication.R;
import com.example.distillapplication.databinding.FragmentSetDistillVolumeBinding;
import com.example.distillapplication.ui.fragment.base.BaseFragment;
import com.example.distillapplication.hardware.protcal.LogString;

/**
 * A simple {@link Fragment} subclass.
 */
public class SetDistillVolumeFragment extends BaseFragment {
    FragmentSetDistillVolumeBinding fragmentSetDistillVolumeBinding;

    public SetDistillVolumeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_set_distill_volume, container, false);
        fragmentSetDistillVolumeBinding = FragmentSetDistillVolumeBinding.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if(mSerialMessageViewModel.onDistillFlag.getValue() == 2){
            int percent = 0;
            double curSteam = mSerialMessageViewModel.curSteamVolume.getValue() - mSerialMessageViewModel.startSteamVolume.getValue();
            double totSteam = mSerialMessageViewModel.targetSteamVolume.getValue() - mSerialMessageViewModel.startSteamVolume.getValue();
            if(curSteam >= totSteam){
                percent = 100;
                mSerialMessageViewModel.onDistillFlag.postValue(0);
                mSharedViewModel.setOperationData(LogString.steamVolumeFinish);
            }else {
                percent = (int) (curSteam / totSteam * 100);
                fragmentSetDistillVolumeBinding.panView.setPercent(percent);
            }
        }

        fragmentSetDistillVolumeBinding.plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int volume;
                if(fragmentSetDistillVolumeBinding.steamVolume.getText().toString().equals("")){
                    volume = 1;
                    fragmentSetDistillVolumeBinding.steamVolume.setText(String.valueOf(volume));
                    return;
                }
                volume = Integer.valueOf(fragmentSetDistillVolumeBinding.steamVolume.getText().toString());
                volume ++ ;
                fragmentSetDistillVolumeBinding.steamVolume.setText(String.valueOf(volume));
            }
        });

        fragmentSetDistillVolumeBinding.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int volume;
                if(fragmentSetDistillVolumeBinding.steamVolume.getText().toString().equals("") || fragmentSetDistillVolumeBinding.steamVolume.getText().toString().equals("0")){
                    return;
                }
                volume = Integer.valueOf(fragmentSetDistillVolumeBinding.steamVolume.getText().toString());
                volume -- ;
                fragmentSetDistillVolumeBinding.steamVolume.setText(String.valueOf(volume));
            }
        });

        fragmentSetDistillVolumeBinding.setup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String steamVolume = fragmentSetDistillVolumeBinding.steamVolume.getText().toString();
                if(steamVolume.equals("")){
                    showLongToast("请输入蒸馏量！");
                }else {
                    fragmentSetDistillVolumeBinding.panView.setMaxNum(Integer.valueOf(steamVolume));
                    fragmentSetDistillVolumeBinding.panView.setStartNum(0);
                    fragmentSetDistillVolumeBinding.panView.setUnit("g");
                    fragmentSetDistillVolumeBinding.panView.setTextSize(20);
                    mSharedViewModel.setOperationData(LogString.setVolume + steamVolume + "g \n");
                    double startVol = mSerialMessageViewModel.curSteamVolume.getValue();
                    mSerialMessageViewModel.startSteamVolume.postValue(startVol);
                    double targetVol = startVol + Double.valueOf(steamVolume);
                    mSerialMessageViewModel.targetSteamVolume.postValue(targetVol);
                }
            }
        });

        mSerialMessageViewModel.onDistillFlag.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if(integer == 0){
                    fragmentSetDistillVolumeBinding.steamVolume.setText("");
                }
            }
        });

        mSerialMessageViewModel.curSteamVolume.observe(getViewLifecycleOwner(), new Observer<Double>() {
            @Override
            public void onChanged(Double aDouble) {
                if(mSerialMessageViewModel.onDistillFlag.getValue() == 2){
                    int percent = 0;
                    double curSteam = mSerialMessageViewModel.curSteamVolume.getValue() - mSerialMessageViewModel.startSteamVolume.getValue();
                    double totSteam = mSerialMessageViewModel.targetSteamVolume.getValue() - mSerialMessageViewModel.startSteamVolume.getValue();
                    if(curSteam >= totSteam){
                        percent = 100;
                        mSerialMessageViewModel.onDistillFlag.postValue(0);
                        mSharedViewModel.setOperationData(LogString.steamVolumeFinish);
                    }else {
                        percent = (int) (curSteam / totSteam * 100);
                        fragmentSetDistillVolumeBinding.panView.setPercent(percent);
                    }
                }
            }
        });


        fragmentSetDistillVolumeBinding.start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSerialMessageViewModel.onDistillFlag.getValue() != 0){
                    showLongToast("当前正在蒸馏过程中，请等待蒸馏完毕！");
                }else if(mSerialMessageViewModel.targetSteamVolume.getValue() <= mSerialMessageViewModel.startSteamVolume.getValue()){
                    showLongToast("请先设置蒸馏量！");
                }else {
                    mSerialMessageViewModel.onDistillFlag.postValue(2);
                    mSharedViewModel.setOperationData(LogString.start);
                }

            }
        });

    }
}
